/**
 * 获取浏览器地址栏各项参数
 */
let urlParam = {
    href: function() {
        var href = window.document.location.href;
        if (href.indexOf("?") > -1) {
            var params = href.split("?")[1];
            var paramArr = params.split('&');
            var res = {};
            for (var i = 0; i < paramArr.length; i++) {
                var str = paramArr[i].split('=');
                res[str[0]] = str[1];
            }
            return res;
        } else {
            return false;
        }
    },
    appToken: null,
    domain: function() {
        return "http://api.juyihome.com/";
    },
    token: function() {
        return "576d49ef-2a2f-4eb9-9ebe-59cbee9856ec"
    },
    productId: function() {
        return this.href().productId;
    },
    sortId: function() {
        return this.href().sortId;
    },
    getAppToken: function(val) {
        alert(val)
        this.appToken = val;
    },
    getAndroidToken: function() {
        if (browser.versions.android) {
            let token = window.AndroidWebView.getAndroidToken();
            this.appToken = token;
        }
    },
}

//调用ios 和 andrid接口
var browser = {
    versions: function() {
        var u = navigator.userAgent,
            app = navigator.appVersion;
        return { //移动终端浏览器版本信息 
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 
            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器 
            iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器 
            iPad: u.indexOf('iPad') > -1, //是否iPad 
        };
    }()
}

function connectWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) {
        callback(WebViewJavascriptBridge)
    } else {
        document.addEventListener('WebViewJavascriptBridgeReady', function() {
            callback(WebViewJavascriptBridge)
        }, false)
    }
}

var myBridge;

connectWebViewJavascriptBridge(function(bridge) {
    bridge.init(function(message, responseCallback) {
        log('JS got a message', message)
        var data = {
            'Javascript Responds': 'Wee!'
        }
        log('JS responding with', data)
        responseCallback(data)
    })


    bridge.registerHandler('testJavascriptHandler', function(data, responseCallback) {
        log('ObjC called testJavascriptHandler with', data)
        var responseData = {
            'Javascript Says': 'Right back atcha!'
        }
        log('JS responding with', responseData)
        responseCallback(responseData)
    })
    myBridge = bridge;
});

function setupWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) {
        return callback(WebViewJavascriptBridge);
    }
    if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback);
    }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() {
        document.documentElement.removeChild(WVJBIframe)
    }, 0)
}


// 点击返回
function globalBack() {
    if (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) {
        window.webkit.messageHandlers.globalBack.postMessage('');
    }
    if (browser.versions.android) {
        window.AndroidWebView.globalBack();
    }
}

//跳转到购物车
function toShopCart() {
    if (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) {
        window.webkit.messageHandlers.appShoppingCartsPush.postMessage('');
    }
    if (browser.versions.android) {
        window.AndroidWebView.appShoppingCartsPush();
    }
}

//跳转到消息列表
function toMessage() {
    if (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) {
        window.webkit.messageHandlers.appMessagePush.postMessage('');
    }
    if (browser.versions.android) {
        window.AndroidWebView.appMessagePush();
    }
}